<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件
// 获取权限列表
function getAuthRule($id=0){
	if($id == 0){
		$data = db('auth_rule')->where('pid',0)->where('status',1)->select();
	}else{
		$data = db('auth_rule')->where('pid',$id)->where('status',1)->select();
	}
	return $data;
}
//获取导航
function getNav(){
	$userinfo = session('userinfo');	
	$nav = db('auth_rule')->where('pid',0)->where('status',1)->where('ismenu',1)->select();	
	if($userinfo['group_id'] == 1){	
		foreach ($nav as $key => $va) {
			$child_nav = db('auth_rule')->where('pid',$va['id'])->where('ismenu',1)->where('status',1)->select();
			if(!$child_nav){
				unset($nav[$key]);
			}else{
				$nav[$key]['child'] = $child_nav;
			}
		}
	}else{
		$rules = db('auth_group')->where('id',$userinfo['group_id'])->value('rules');
		foreach ($nav as $key => $va) {
			$child_nav = db('auth_rule')->where('pid',$va['id'])->where('id','in',$rules)->where('ismenu',1)->where('status',1)->select();
			if(!$child_nav){
				unset($nav[$key]);
			}else{
				$nav[$key]['child'] = $child_nav;
			}
		}
	}	
	return $nav;
}

//时间戳转化时间格式
function nicetime($time,$create_time){
	if(!$time){
		return '00时00分';
	}
	$now_time = time();
	if(($create_time+$time)<$now_time){
		return '00时00分';
	}
	$time = $create_time+$time-$now_time;
	// return $time;
	$hours = intval($time/3600);
	$min =  intval(($time-$hours*3600)/60);
	return $hours.'时'.$min.'分';
}

//获取字符串第一个字符
function getFirstChar($string){
	$string = mb_substr( $string, 0, 1,'utf-8');
	return $string;
}

function getTags($id){
	return db('tags')->where('id',$id)->find();
}

function cut_str($str, $len){ 
  $one=0;
  $partstr='';
  for($i=0;$i<$len;$i++)
{ $sstr=substr($str,$one,1);
 if(ord($sstr)>224){
 $partstr.=substr($str,$one,3);
 $one+=3;
 }elseif(ord($sstr)>192){
 $partstr.=substr($str,$one,2);
 $one+=2;
 }elseif(ord($sstr)<192){
 $partstr.=substr($str,$one,1);
 $one+=1;
 }
}
if(strlen($str)<$one){
   return $partstr;}else{
return $partstr.'....';
}
}

function getMid($uid){
	if(!$username = db('user')->where('id',$uid)->value('username')){
		return '无';
	}else{
        return $username;
	}
}

function getGroup($group_uid){
	if(!$title = db('auth_group')->where('id',$group_uid)->value('title')){
		return '无';
	}else{
        return $title;
	}
}

function getName($uid){
	if(!$name = db('user')->where('id',$uid)->value('name')){
		return '无';
	}else{
        return $name;
	}
}